/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 *
 * @author Admin
 */

@Entity
@Table(name = "Student")
@NamedQueries({
    @NamedQuery(name = "Student.findAll", query = "SELECT s FROM Student s"),
    @NamedQuery(name = "Student.findByStudentId", query = "SELECT s FROM Subject s WHERE s.id = :studentId"),
    @NamedQuery(name = "Student.findByProgram", query = "SELECT s FROM Student s WHERE s.program = :program"),
    @NamedQuery(name = "Student.findByPerson", query = "SELECT s FROM Student s WHERE s.person = :person")
})
public class Student implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "StudentId")
    private Long id;
    
    @JoinColumn(name = "ProgramId", referencedColumnName = "ProgramId")
    @ManyToOne(optional = true, cascade = CascadeType.PERSIST)
    private Program program;
    
    @JoinColumn(name = "PersonId", referencedColumnName = "PersonId")
    @OneToOne(optional = true, cascade = CascadeType.ALL)
    private Person person;
    
//    @JoinTable(name = "author_book",
//    joinColumns = {
//    @JoinColumn(name="authorId") 
//    },
//    inverseJoinColumns = {
//    @JoinColumn(name="bookId")
//        }
//    )
    
    
    @ManyToMany(cascade= CascadeType.PERSIST, mappedBy = "students")
    @JoinTable(name = "StudentSubject", joinColumns = { @JoinColumn(name="studentId")}, 
            inverseJoinColumns = {@JoinColumn(name="subjectId")})
    private List<Subject> subjects;
    
    public Student(){
    }

    public Long getId() {
        return id;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Program getProgram() {
        return program;
    }

    public void setProgram(Program program) {
        this.program = program;
    }

    public List<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<Subject> subjects) {
        this.subjects = subjects;
    }
    
    public void addSubject(Subject subject){
        if(subjects == null){
            subjects = new ArrayList<Subject>();
        }
        this.subjects.add(subject);
    }
    
    public void removeSubject(Subject subject){
        this.subjects.remove(subject);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Student)) {
            return false;
        }
        Student other = (Student) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return person.getFirstName() + " " + person.getLastName() + " (" + id + ")";
    }
    
}
